package service

import (
	"context"
	"errors"
	"github.com/google/uuid"
	__ "service/basic/proto"
	"service/handler/model"
	"service/untils"
	"strconv"
)

// 处方审核
func (s *Server) ResStatus(_ context.Context, in *__.ResStatusReq) (*__.ResStatusResp, error) {
	//接收参数
	var res model.Prescription
	res.Id = uint(in.Id)
	res.PreStstus = uint(in.ResStatus)
	//审核
	if err := res.UpdatePreStatus(); err != nil {
		return nil, errors.New("处方审核成功")
	}

	return &__.ResStatusResp{
		Id: int64(res.Id),
	}, nil
}

// 订单创建
func (s *Server) OrderAdd(_ context.Context, in *__.OrderAddReq) (*__.OrderAddResp, error) {
	//接收参数
	order := model.Prescription{
		OrderNo:  uuid.NewString(),
		UserId:   uint(in.UserId),
		DockerId: uint(in.DockerId),
		Medicine: in.Medicine,
		Stock:    uint(in.Stock),
		Price:    29.5,
	}
	if err := order.Create(); err != nil {
		return nil, errors.New("订单创建失败")
	}
	outTradeNo := uuid.NewString()
	totalAmount := order.Price
	pay := untils.AliPay(outTradeNo, strconv.FormatInt(int64(totalAmount), 10))
	return &__.OrderAddResp{
		Pay: pay,
	}, nil
}
